function gadget:GetInfo()
	return {
		name = "Chain Weapon",
		desc = "Makes certain weapons chain",
		author = "CarRepairer",
		date = "2000-11-09",
		license   = "GNU GPL, v2 or later",
		layer = 1,
		enabled = true,
	}
end


if (gadgetHandler:IsSyncedCode()) then
--SYNCED

local spGetUnitPosition     = Spring.GetUnitPosition
local spGetUnitVelocity     = Spring.GetUnitVelocity
local spGetUnitVelocity     = Spring.GetUnitVelocity
local spGetUnitSeparation   = Spring.GetUnitSeparation

local echo   = Spring.Echo

local chainWeapons = {}
local fakeShooters = {}
local addFakeShooters = {}
local remFakeShooters = {}
local chainMasters = {}
local linkedTargets = {}



unitCheck = {}

local wizardDefID = UnitDefNames['wizard'].id

function gadget:Initialize()
	for i=1,#WeaponDefs do
		if (WeaponDefs[i].name == 'wizard_lb1' ) then
			chainWeapons[i] = 3
		end
	end
end


function gadget:UnitDamaged(unitID, unitDefID, unitTeam, damage, paralyzer, weaponID, attackerID, attackerDefID, attackerTeam)
	if not chainWeapons[weaponID] then return end
	
	if Spring.AreTeamsAllied(unitTeam, attackerTeam) then return end
	
	
	local level 
	
	if fakeShooters[attackerID] then 
		remFakeShooters[attackerID] = 32 --1 second
		level = fakeShooters[attackerID]-1
		chainMaster = chainMasters[attackerID]
	end
	
	if unitCheck[unitID] then return end
	
	if not fakeShooters[attackerID] then 
		chainMaster = attackerID
		--echo ('adding', chainMaster, unitID)
		linkedTargets[chainMaster] = {[unitID] = true}
		level = 3
	end
	
	if level == 0 then return end
	
	unitCheck[unitID] = Spring.GetGameFrame()
	
	
	addFakeShooters[unitID] = {attackerDefID,attackerTeam, 	unitID, unitTeam, level,	chainMaster }
	
end

function gadget:GameFrame(f)

	for unitID, _ in pairs(unitCheck) do
		if f > unitCheck[unitID] + 60 then
			unitCheck[unitID] = nil
		end
	end

	for unitID,data in pairs(addFakeShooters) do
	
		
		local fakeShooterDefID = data[1]
		local fakeShooterTeam = data[2]
		local targetID = data[3]
		local targetTeam = data[4]
		local level = data[5]
		
		local chainMaster = data[6]
		
		
		local x,y,z = spGetUnitPosition(targetID)
	
		--local targetID = Spring.GetUnitNearestAlly(targetID, 500) -- HUH? SAME UNIT?!
		--echo('nearest', targetID, targetID)
		
		local target2ID
		local othertargets = Spring.GetUnitsInCylinder(x,z,500, targetTeam)
		for _,otherID in ipairs(othertargets) do
			if otherID ~= targetID 
				and not linkedTargets[chainMaster][otherID]
				then
				target2ID = otherID
			end
		end
		
		if target2ID then
			
			local fakeUnitID = Spring.CreateUnit(fakeShooterDefID, x,y,z, 0, fakeShooterTeam)
			Spring.SetUnitMaxHealth(fakeUnitID,99999)
			Spring.SetUnitHealth(fakeUnitID,99999)
			Spring.SetUnitCollisionVolumeData(fakeUnitID, 0,0,0,   0,0,0,  0,0,0)
			
			local pieces = Spring.GetUnitPieceMap(fakeUnitID)
			local wandNum = pieces['wand']
			local px,py,pz = Spring.GetUnitPiecePosition(fakeUnitID, wandNum)
			
			fakeShooters[fakeUnitID] = level
			chainMasters[fakeUnitID] = chainMaster
			
			--echo ('chainMaster',chainMaster, target2ID)
			linkedTargets[chainMaster][target2ID] = true
			
			local tx,ty,tz = spGetUnitPosition(target2ID)
			local dist = Spring.GetUnitSeparation(targetID, target2ID)
			local dx = (tx-x) / dist
			local dy = (ty-y) / dist
			local dz = (tz-z) / dist
			
			local t1rad = Spring.GetUnitRadius(targetID) + 10
			
			local MC = Spring.MoveCtrl
			MC.Enable(fakeUnitID)
			MC.SetPosition(fakeUnitID, x -px +dx*t1rad, y -py +dy*t1rad, z -pz +dz*t1rad)
			
			Spring.SetUnitCloak(fakeUnitID, 4)
			SendToUnsynced('hidefakeunit', fakeUnitID)
			Spring.SetUnitBlocking(fakeUnitID, true)
			Spring.SetUnitTarget(fakeUnitID, target2ID)
		end
		
	end
	
	addFakeShooters = {}
	
	for fakeUnitID,_ in pairs(remFakeShooters) do
		if remFakeShooters[fakeUnitID] > 0 then
			remFakeShooters[fakeUnitID] = remFakeShooters[fakeUnitID] - 1
		else
			Spring.DestroyUnit(fakeUnitID, false, true) 
			remFakeShooters[fakeUnitID] = nil
			fakeShooters[fakeUnitID] = nil
			chainMasters[fakeUnitID] = nil
		end
	end
	
end

------ SYNCED ----------
else
------ UNSYNCED --------

local spSetUnitNoMinimap    = Spring.SetUnitNoMinimap
local spSetUnitNoSelect     = Spring.SetUnitNoSelect
local spSetUnitNoDraw       = Spring.SetUnitNoDraw

local function HideFakeUnit(cmd, unitID)	
	if (type(unitID) ~= 'number') then
		return
	end
	spSetUnitNoDraw(unitID, true)
	spSetUnitNoMinimap(unitID, true)
	spSetUnitNoSelect(unitID, true)
end

function gadget:Initialize()
  gadgetHandler:AddSyncAction('hidefakeunit', HideFakeUnit)
end

function gadget:Shutdown(unitID)
  gadgetHandler:RemoveSyncAction('hidefakeunit') 
end

------ UNSYNCED ----------
end










